package pkg

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func File() {
	router := gin.Default()
	// 为 multipart forms 设置较低的内存限制 (默认是 32 MiB)
	router.MaxMultipartMemory = 8 << 20 // 8 MiB
	router.POST("/upload", func(c *gin.Context) {
		// 单文件
		file, _ := c.FormFile("file")
		log.Println(file.Filename)
		if file.Size > 5*1024*1024 {
			c.JSON(500, gin.H{
				"code": 500,
				"msg":  "视频超过50mb",
			})
		}
		if file.Filename != ".MP4" {
			c.JSON(500, gin.H{
				"code": 500,
				"msg":  "视频格式不正确",
			})
		}
		// 上传文件至指定的完整文件路径
		err := c.SaveUploadedFile(file, "./files/"+file.Filename)
		if err != nil {
			c.JSON(500, gin.H{
				"code": 500,
				"msg":  "视频文件上传失败",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "视频文件上传成功",
		})
	})
	router.Run(":8080")
}
